package com.anywhere.freightman.order.service.impl;

import com.anywhere.freightman.common.BaseEntityService;
import com.anywhere.freightman.common.BaseMapper;
import com.anywhere.freightman.common.entity.order.AddressBookQueryEntity;
import com.anywhere.freightman.order.dao.AddressBookDao;
import com.anywhere.freightman.order.model.AddressBookModel;
import com.anywhere.freightman.order.service.IAddressBookService;
import com.google.common.base.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.weekend.Weekend;
import tk.mybatis.mapper.weekend.WeekendCriteria;

import java.util.List;

@Service
@Transactional
public class AddressBookService extends BaseEntityService<AddressBookModel, AddressBookQueryEntity> implements IAddressBookService {

    @Autowired
    private AddressBookDao addressBookDao;

    @Override
    public BaseMapper<AddressBookModel> getBaseDao() {
        return addressBookDao;
    }

    @Override
    public Example getExample(AddressBookQueryEntity entity) {
        Weekend<AddressBookModel> weekend = Weekend.of(AddressBookModel.class);
        WeekendCriteria<AddressBookModel, Object> criteria = weekend.weekendCriteria();
        if (entity.getCompanyId() != null && entity.getCompanyId() > 0) {
            criteria.andEqualTo("companyId", entity.getCompanyId());
        }

        if (!Strings.isNullOrEmpty(entity.getName())) {
            criteria.andLike("name", "%" + entity.getName() + "%");
        }
        return weekend;
    }


    @Override
    public List<AddressBookModel> getList(Long companyId) {
        return addressBookDao.getList(companyId);
    }

    @Override
    public AddressBookModel getByTel(String tel) {
        return addressBookDao.getByTel(tel);
    }
}
